/**
 * @class AddDepartmentWin
 * 添加院系信息的一个窗口
 */
DC.front.SysManagerWin = function(params, config){
	this.myUserForm = new DC.front.ModifyUserFormPanel();
	
	this.modifyPasswordForm = new DC.front.ModifyPasswordFormPanel();
	
	DC.front.SysManagerWin.superclass.constructor.call(this, Ext.apply({
    	id: 'sys_manger_win',
    	title : 'Mantain User Info Win',
        width: 500,
        height: 230,
        plain: true,
        border: false,
        bodyBorder: true,
        layout: 'fit',
        closeAction : 'close',
        modal: true,
        items :[{
        	xtype : 'tabpanel',
    		margins : '0 0 0 0',
    		cmargins : '0 0 0 0',
    		border : true,
    		bodyBorder : false,
    		resizeTabs : false,
            tabWidth : 100,
            minTabWidth : 100,
            enableTabScroll : true,
            activeTab : 0,
    		items: [ this.myUserForm, this.modifyPasswordForm ]
        }],
        listeners: {
        	afterRender: function() { // 当窗口渲染完成后执行
        		
        	},
        	beforeDestroy: function() { //关闭窗口前销毁窗口包含的组件
        		/*if(this.rendered) {
        		    if(this.myUserForm){
				        this.remove(this.myUserForm);
			        }
			        DC.util.CommonAction.doDestroyComponent();
        		}*/
        	}
        }
    }, config ||{}));
};

Ext.extend(DC.front.SysManagerWin, Ext.Window);

/**
 * 添加表单panel
 * @class AddDepartmentFormPanel
 * @param {} config
 */
DC.front.ModifyUserFormPanel = function(params, config) {
	var mufp = this;
	
	var mbool= true, fbool = false;
	if(DC.front.variable.SYS_CURRENTUSER.sex == 1) {
		mbool = false;
		fbool = true;
	}
	
	DC.front.ModifyUserFormPanel.superclass.constructor.call(this, Ext.apply({
            id: 'modifyUser_formPanel',
            title : 'Modify User Info',
            labelWidth: 70,
            labelAlign: 'right',
            border : false,
            frame: true,
            buttonAlign : 'center',
            items: [{
            	layout : 'form',
            	defaultType : 'textfield',
            	defaults : {
                    width : 380
                },
            	items : [{
            		    id : 'modifyUser_formPanel_id',
		                name : 'id',
		                xtype : 'hidden',
		                value : DC.front.variable.SYS_CURRENTUSER.id
		            }, {
		            	id : 'modifyUser_formPanel_username',
		            	xtype : 'label',
		                name : 'username',
		                fieldLabel : 'User Name',
		                text : DC.front.variable.SYS_CURRENTUSER.username
		            }/*, {
		            	id: 'editUser_formPanel_email',
		            	name: 'email',
		                fieldLabel: '<font color="red">*</font>Email',
		                allowBlank: false, // 表单控件不可以为空
		                blankText: "Please enter you email.",
		                regex: /^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*(\;)*\s*$/,
		                regexText: "The email String is Illegal."
            	}*/]
            }, {
            	xtype : 'radiogroup',
            	fieldLabel : 'Gender',
            	items : [ {
            		id : 'modifyUser_formPanel_gender_male',
        			name : 'gender',
        			boxLabel : 'Male',
        			checked : mbool,
        			value : 'male'
        		}, {
        			id : 'modifyUser_formPanel_gender_female',
            		name : 'gender',
            		boxLabel : 'Female',
            		checked : fbool,
        			value : 'female'
        		}]
            }, {
            	layout : 'form',
            	defaults: {
                    width: 380,
                },
            	items : [{
            		id : 'modifyUser_formPanel_birthday',
            		xtype : 'datefield',
        			name : 'birthday',
        			fieldLabel : 'Birthday',
        			format : 'Y-m-d',
        			value : DC.front.variable.SYS_CURRENTUSER.birthday
        	    }]
        }],
        buttons : [{
        	text : 'Save',
        	handler : function() {
        		mufp.modifyUserInfo();
        	}
        }, {
        	text : 'Reset',
			handler : function() {
			    Ext.getCmp('modifyUser_formPanel_id').setValue(DC.front.variable.SYS_CURRENTUSER.id);
			    Ext.getCmp('modifyUser_formPanel_username').setValue(DC.front.variable.SYS_CURRENTUSER.username);
			    Ext.getCmp('modifyUser_formPanel_birthday').setValue(DC.front.variable.SYS_CURRENTUSER.birthday);
			}
        }]
    }, config ||{}));
};

Ext.extend(DC.front.ModifyUserFormPanel, Ext.form.FormPanel, {
    // 修改用户信息的响应事件
    modifyUserInfo: function(id) { // 修改当前用户
    	if (this.getForm().isValid()) {
    		this.getForm().submit({
                url: '',
                success: function(form, action){
                	var res = action.result;
                	DC.util.CommonAction.unMask();
                	if (res.success) {
                		DC.front.variable.SYS_CURRENTUSER = res.userInfo;
                		Ext.getCmp("sys_manger_win").close();
                		Ext.example.msg('Prompt', 'Modify successful.', 'prompt');
                	} else {
                		Ext.example.msg('Prompt', res.msg, res.type);
                	}
                },
                failure: function(form, action){
                	DC.util.CommonAction.unMask();
                	switch (action.failureType) {
		                case Ext.form.Action.CLIENT_INVALID: // 当客户端验证失败时执行
		                     break;
		                case Ext.form.Action.CONNECT_FAILURE:
		                	 Ext.example.msg('Prompt Win', 'Network connection failure, so fail to commit.', 'error');
		                     break;
		                case Ext.form.Action.SERVER_INVALID: // 当服务器端的success属性的值返回false时执行
		                	 Ext.example.msg('Prompt Win', action.result.message, 'error');
		            }
                }
            });
    	}
    }
});

DC.front.ModifyPasswordFormPanel = function(params, config) {
	var mpfp = this;
	DC.front.ModifyPasswordFormPanel.superclass.constructor.call(this, Ext.apply({
           id: 'modifyPassword_formPanel',
           title : 'Modify Password',
           labelWidth: 120,
           labelAlign: 'right',
           buttonAlign : 'center',
           frame: true,
           defaultType: 'textfield',
           defaults: {
               width: 340,
               labelStyle: 'margin:5px auto'
           },
           items: [/*{
           	   xtype : 'label',
           	   id : 'modifyPassword_formPanel_tip',
           	   height : 100,
               name : 'msg',
               fieldLabel : 'Tip',
               hideLabel : true,
               text : 'modifyPassword_formPanel'
           },*/ {
           	   inputType: 'password',
           	   id: 'modifyPassword_formPanel_new_password',
           	   name: 'newPassword',
               fieldLabel: '<font color="red">*</font>New Password',
               style:'margin:5px auto',
               allowBlank: false, // 表单控件不可以为空
               blankText:"Please enter your new password.",
               maxLength: 25,
               maxLengthText : 'The length of the old password cannot be more than 25.'
           }, {
           	   inputType: 'password',
           	   id: 'modifyPassword_formPanel_ok_password',
               name: 'okPassword',
               fieldLabel: '<font color="red">*</font> Re-enter Password',
               style:'margin:5px auto',
               allowBlank: false, // 表单控件不可以为空
               blankText:"Please enter your new password again.",
               maxLength: 25,
               maxLengthText : 'The length of the Re-enter password cannot be more than 25.'
           }, {
           	   inputType: 'password',
           	   id: 'modifyPassword_formPanel_old_password',
           	   name: 'oldPassword',
               fieldLabel: '<font color="red">*</font>Old Password',
               style:'margin:5px auto',
               allowBlank: false, // 表单控件不可以为空
               blankText:"Please enter your old password.",
               maxLength: 25,
               maxLengthText : 'The length of the old password cannot be more than 25.'
           }],
           buttons : [{
	           	text : 'Save',
	           	handler : function() {
	           		mpfp.modifyPassword();
	           	}
           }, {
           	text : 'Reset',
   			handler : function() {
   				mpfp.getForm().reset();
   			}
       }]
   }, config ||{}));
};

Ext.extend(DC.front.ModifyPasswordFormPanel, Ext.form.FormPanel, {
	 // 添加用户的响应事件
	modifyPassword: function() {
   	   // 判断两次输入的密码是否相等
		var newPassVal = Ext.getCmp("modifyPassword_formPanel_new_password").getValue().trim();
		var okPassVal = Ext.getCmp("modifyPassword_formPanel_ok_password").getValue().trim();
		var oldPassVal = Ext.getCmp("modifyPassword_formPanel_old_password").getValue().trim();
		if(newPassVal != null && okPassVal != null && newPassVal != okPassVal){
		    Ext.getCmp("modifyPassword_formPanel_ok_password").setValue('');
		    //Ext.example.msg('Prompt', res.msg, 'prompt');
			return ;
		}
		if(oldPassVal != DC.front.variable.SYS_CURRENTUSER.password) {
			Ext.example.msg('Prompt', 'The old password is wrong.', 'warning');
			return;
		}
		if (this.getForm().isValid()) {
			DC.util.CommonAction.loadMask("Saveing...");
			this.getForm().submit({
		           clientValidation: true,
		           method: 'post',
		           url: '',
		           success: function(form, action){
		        	   var res = action.result;
	                	DC.util.CommonAction.unMask();
	                	if (res.success) {
	                		DC.front.variable.SYS_CURRENTUSER = res.userInfo;
	                		Ext.getCmp("sys_manger_win").close();
	                		Ext.example.msg('Prompt', 'Modify successful.', 'prompt');
	                	} else {
	                		Ext.example.msg('Prompt', res.msg, res.type);
	                	}
		           },
		           failure: function(form, action){
		        	   DC.util.CommonAction.unMask();
		               switch (action.failureType) {
			                case Ext.form.Action.CLIENT_INVALID: // 当客户端验证失败时执行
			                     break;
			                case Ext.form.Action.CONNECT_FAILURE:
			                	 Ext.example.msg('Prompt Win', 'Network connection failure, so fail to commit.', 'error');
			                     break;
			                case Ext.form.Action.SERVER_INVALID: // 当服务器端的success属性的值返回false时执行
			                	 Ext.example.msg('Prompt Win', action.result.message, 'error');
			            }
		           }
		     });
		}
   },
   cleanAllInput : function() {
	   Ext.getCmp("modifyPassword_formPanel_new_password").setValue('');
	   Ext.getCmp("modifyPassword_formPanel_ok_password").setValue('');
	   Ext.getCmp("modifyPassword_formPanel_old_password").setValue('');
   }
});